gui/macOS: Avoid need to retain/release things in file provider edit locally
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Thu, 27 Mar 2025 10:45:13 +0000 (11:45 +0100)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Thu, 27 Mar 2025 14:16:05 +0000 (14:16 +0000)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/macOS/fileprovidereditlocallyjob_mac.mm

index 28ff7fd5b237a9f9ec80b965a5405643e0f1e704..ec5ffcc755b0fdc17acbf4cad93e1382a932bf20 100644 (file)
@@ -44,8 +44,7 @@ void FileProviderEditLocallyJob::openFileProviderFile(const QString &ocId)
     
     NSFileProviderDomain *const domain = (NSFileProviderDomain *)voidDomain;
     if (domain == nil) {
-        qCWarning(lcFileProviderEditLocallyMacJob) << "Could not get domain for account:"
-                                                   << userId;
+        qCWarning(lcFileProviderEditLocallyMacJob) << "Could not get domain for account:" << userId;
         emit notAvailable();
     }
 
@@ -56,40 +55,33 @@ void FileProviderEditLocallyJob::openFileProviderFile(const QString &ocId)
         emit notAvailable();
     }
 
-    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
-    __block NSError *receivedError;
-    __block NSURL *itemLocalUrl;
     [manager getUserVisibleURLForItemIdentifier:nsOcId
                               completionHandler:^(NSURL *const url, NSError *const error) {
-        [url retain];
-        [error retain];
-        itemLocalUrl = url;
-        receivedError = error;
-        dispatch_semaphore_signal(semaphore);
-    }];
-    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
-
-    Systray::instance()->destroyEditFileLocallyLoadingDialog();
 
-    if (receivedError != nil) {
-        const auto errorMessage = QString::fromNSString(receivedError.localizedDescription);
-        qCWarning(lcFileProviderEditLocallyMacJob) << "Error getting user visible URL for item"
-                                                   << ocId << ":" << errorMessage;
-        emit notAvailable();
-    } else if (itemLocalUrl != nil) {
-        const auto itemLocalPath = QString::fromNSString(itemLocalUrl.path);
-        qCDebug(lcFileProviderEditLocallyMacJob) << "Got user visible URL for item" 
-                                                 << ocId << ":" << itemLocalPath;
-        [NSWorkspace.sharedWorkspace openURL:itemLocalUrl];
-        emit finished();
-    } else {
-        qCWarning(lcFileProviderEditLocallyMacJob) << "Got nil user visible URL for item"
-                                                   << ocId;
-        emit notAvailable();
-    }
+        dispatch_async(dispatch_get_main_queue(), ^{
+            Systray::instance()->destroyEditFileLocallyLoadingDialog();
+        });
 
-    [itemLocalUrl release];
-    [receivedError release];
+        if (error != nil) {
+            const auto errorMessage = QString::fromNSString(error.localizedDescription);
+            qCWarning(lcFileProviderEditLocallyMacJob) << "Error getting user visible URL for item:" << errorMessage;
+            dispatch_async(dispatch_get_main_queue(), ^{
+                emit notAvailable();
+            });
+        } else if (url != nil) {
+            const auto itemLocalPath = QString::fromNSString(url.path);
+            qCDebug(lcFileProviderEditLocallyMacJob) << "Got user visible URL for item:" << itemLocalPath;
+            [NSWorkspace.sharedWorkspace openURL:url];
+            dispatch_async(dispatch_get_main_queue(), ^{
+                emit finished();
+            });
+        } else {
+            qCWarning(lcFileProviderEditLocallyMacJob) << "Got nil user visible URL for item" << ocId;
+            dispatch_async(dispatch_get_main_queue(), ^{
+                emit notAvailable();
+            });
+        }
+    }];
 }
 
 } // namespace OCC::Mac